#include "FoodSpriteManager.h"
#include "GameScene.h"
#include "SimpleAudioEngine.h"   

using namespace CocosDenshion;

static FoodSpriteManager *s_SharedFoodSpriteManager = NULL;
CCSpriteBatchNode* batchNode ;
FoodSpriteManager* FoodSpriteManager::sharedFoodSpriteManager(void)
{
	if (!s_SharedFoodSpriteManager)
	{
		s_SharedFoodSpriteManager  =new FoodSpriteManager();
		s_SharedFoodSpriteManager->init();
		/*	foodDict =CCDictionary::create();
		FoodType eNum = kTypeWatermelon;
		for (eNum = kTypeWatermelon; eNum < kTypeBomb; eNum =(FoodType) (eNum + 1))
		{

		} */
	}
	return s_SharedFoodSpriteManager;
}

bool FoodSpriteManager::init()
{
	bool bRet = false;
	do 
	{
		CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("Items02.plist");

		//batchNode =CCSpriteBatchNode::create("collectibles.png");
	    batchNode = new CCSpriteBatchNode();
        batchNode->initWithFile("collectibles.png", kDefaultSpriteBatchCapacity);
		bRet= true;
	}while (0);
	return bRet;
}

FoodSprite* FoodSpriteManager::createFood(FoodType foodtype)
{
	FoodSprite * foodsprite =FoodSprite::create();
	foodsprite->setType(foodtype);
	char tmpstr[100] = {0};
	sprintf(tmpstr, "hlb%d.png",(int)foodtype+1);

	CCRect rect;
	if(foodtype == kTypeWatermelon)
		rect=CCRect(58,302,58,58);
	else if(foodtype == kTypeStrawberry)
		rect=CCRect(290, 290, 58, 58);
	else if(foodtype == kTypePineapple)
		rect=CCRect(58 ,418 ,58, 58);
	else if(foodtype == kTypeGrapes)
		rect=CCRect(58, 360 ,58 ,58);
	else if(foodtype == kTypeBanana)
		rect=CCRect(232, 116, 58 ,58);
	else if(foodtype == kTypeBomb)
		rect=CCRect(290 ,174, 58 ,58);
	else if(foodtype == kType2X)
		rect=CCRect(96 ,476 ,32, 32);
	else if(foodtype == kTypePoison)
		rect=CCRect(32, 476, 32 ,32);
	else if(foodtype == kTypePetrified)
		rect=CCRect(64, 476, 32 ,32); 

	foodsprite->initWithTexture(batchNode->getTexture(),rect);
	return foodsprite;
}



void FoodSpriteManager::showScore(FoodSprite* foodsprite ,int score)
{

	FoodType	foodtype = foodsprite->getType();
	std::string  tmpstr;
	if(score == 2)
		tmpstr="money02.png";
	else if(score == 14)
		tmpstr="money14.png";
	else if(score == 50)
		tmpstr="money50.png";
	else if(score == 150)
		tmpstr="money150.png";
	else if(score == 1000)
		tmpstr="money1000.png";
	else
		tmpstr="money85.png";

	CCSprite *scoreCom =CCSprite::createWithSpriteFrameName(tmpstr.c_str());
	scoreCom->setPosition(ccp(foodsprite->getPositionX(),foodsprite->getPositionY() +foodsprite->getContentSize().height /2+10));
	((CCLayer*)(foodsprite->getParent()))->addChild(scoreCom);

	CCMoveBy * moveby =CCMoveBy::create(0.5f,ccp(0,20));
	CCHide *hide =CCHide::create();
	hide->setDuration(0.5f);
	CCSequence * sequence =CCSequence::create(moveby,hide,NULL);
	scoreCom->runAction(sequence);
}

void FoodSpriteManager::computeFoodSpriteScore(FoodSprite* foodsprite)
{
	FoodType	foodtype = foodsprite->getType();
	int currentPlayerState = GameLayer::sharedGameLayer()->player->getState();
	std::string  tmpstr;
	int addScore =0;
	int tmpSkill =1;
	if(currentPlayerState == kStatePetrified)
	{
		tmpSkill =0;
	}
	else if(currentPlayerState == kState2Point)
	{
		tmpSkill =2;
	}

	if(foodtype == kType2X)
	{
		GameLayer::sharedGameLayer()->updateState(kState2Point);
		SimpleAudioEngine::sharedEngine()->playEffect("GiftBox01.mp3");
	}
	else if(foodtype == kTypePoison)
	{
		GameLayer::sharedGameLayer()->updateState(kStatePoison);
		SimpleAudioEngine::sharedEngine()->playEffect("GiftBox02.mp3");
	}
	else	if(foodtype == kTypePetrified)
	{
		GameLayer::sharedGameLayer()->updateState(kStatePetrified);
		SimpleAudioEngine::sharedEngine()->playEffect("GiftBox03.mp3");
	}
	else if(foodtype == kTypeStrawberry)
	{
		addScore =2 *tmpSkill;
	}
	else if(foodtype == kTypePineapple)
	{
		addScore =14 *tmpSkill;
	}
	else if(foodtype == kTypeGrapes)
	{
		addScore =50 *tmpSkill;
	}
	else if(foodtype == kTypeBanana)
	{
		addScore =150 *tmpSkill;
	}
	else if(foodtype == kTypeBomb)
	{
		addScore =1000 *tmpSkill;
	}
	else if(foodtype == kTypeWatermelon)
	{
		addScore =85 *tmpSkill;
	}



	if(addScore > 0)
	{
		FoodSpriteManager::sharedFoodSpriteManager()->showScore(foodsprite,addScore);
		GameLayer::sharedGameLayer()->addScore(addScore);
		SimpleAudioEngine::sharedEngine()->playEffect("BuyItem.mp3");
	}
}

